package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

//本地服务器连接串
//"数据库用户名:数据库密码@tcp(127.0.0.1)/库名
const sqladdrs = "root:12345678@tcp(127.0.0.1)/test"

//错误处理
func err_ctrl(err error, name string) {
	if err != nil {
		fmt.Println(name, err)
		//停止执行
		return
	}
}

//登陆查询密码
func Read_sql(account string) (string, string, string) {
	db, err := sql.Open("mysql", sqladdrs)
	err_ctrl(err, "sql连接错误：")
	defer db.Close()
	row, err := db.Query("SELECT passwd,id,root FROM member WHERE account=?", account)
	err_ctrl(err, "查询语句错误：")
	var passwd, id, root string
	for row.Next() {
		row.Scan(&passwd, &id, &root)
	}
	return passwd, id, root
}

//验证注册ajax账号重复问题
func RegistercheckAccount(account string) bool {
	db, err := sql.Open("mysql", sqladdrs)
	err_ctrl(err, "sql连接错误")
	defer db.Close()
	row, err := db.Query("SELECT account FROM member WHERE account=?", account)
	err_ctrl(err, "查询错误：")
	var accounts string
	for row.Next() {
		row.Scan(&accounts)
	}
	flage := true
	if accounts == "" {
		flage = false
	}
	return flage
}

//注册信息插入数据库
func InsertRegisterInfo(name string, tel string, QQ_num string, gender string, birthType string, birthday string, drom string, college string, account string, passwd string) bool {
	db, err := sql.Open("mysql", sqladdrs)
	err_ctrl(err, "sql连接错误")
	defer db.Close()
	if gender == "man" {
		gender = "男"
	} else {
		gender = "女"
	}
	if birthType == "1" {
		birthType = "公历"
	} else {
		birthType = "农历"
	}
	if err != nil {
		fmt.Println("插入错误：", err)
		return false
	} else {
		return true
	}
}
